package cn.edu.nju.ws.sview.wyge.relations.adataset.comment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.database.DBOperations;
import cn.edu.nju.ws.sview.util.PropertyList;
import cn.edu.nju.ws.sview.views.Group;
import cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetName;

public class DatasetComment {
	private int commentId = -1;
	private String dataset_type = null;
	private int dataset_id = -1;
	private Date time = null;
//	private String author = null;
	private String groupName = null;
	private String language = null;
	private String text = null;
	
	@Deprecated
	private int likeTotal = -1;
	@Deprecated
	private int dislikeTotal = -1;
	
	public DatasetComment(String groupName, Date time,String lang,String text,String dataset_type,int dataset_id) {
		this.groupName = groupName;
		this.time = time;
		this.language = lang;
		this.text = text;
		this.dataset_type = dataset_type;
		this.dataset_id = dataset_id;
	}
	@Deprecated
	public DatasetComment(int commentId, String dataset_type, int dataset_id, Date time, //String group, 
			String lang, String text, int like_total,
			int dislike_total) {
		this.commentId = commentId;
		this.dataset_type = dataset_type;
		this.dataset_id = dataset_id;
		this.time = time;
//		this.group = group;
		this.language = lang;
		this.text = text;
		this.likeTotal = like_total;
		this.dislikeTotal = dislike_total;
	}

	public int getCommentID() {
		return this.commentId;
	}

	public String getDatasetType() {
		return this.dataset_type;
	}

	public int getDatasetID() {
		return this.dataset_id;
	}

//	public String getGroup() {
//		return group;
//	}

	public Date getTime() {
		return time;
	}

	public String getLang() {
		return this.language;
	}

	public String getText() {
		return this.text;
	}

	public int getLikeTotal() {
		return likeTotal;
	}

	public int getDislikeTotal() {
		return dislikeTotal;
	}

	@Override
	public int hashCode() {
		return time.hashCode() * 31 + dataset_type.hashCode() + dataset_id;//===========
		//return time.hashCode() * 31 +group.hashCode();
	}
	public String toString(){
		return dataset_id+"_"+dataset_type+":"+text+"@"+language;
	}
	@Override
	public boolean equals(Object obj) {
//		return time == ((DatasetComment) obj).time
//				&& author.equals(((DatasetComment) obj).author);
//		return dataset_id == ((DatasetComment) obj).dataset_id
//				&& dataset_type.equals(((DatasetComment) obj).dataset_type);
//				&&group.equals(((DatasetComment) obj).group);
		if (obj == this)
			return true;
		if (!(obj instanceof DatasetName))
			return false;
		DatasetName that = (DatasetName) obj;
		if (that.toString().equals(this.toString())) {
			return true;
		}
		return false;
	}

	public static void insert(String ds_type, int ds_id, String group,
			String lang, String text) {
//		int userId = User.getUserIdByName(author);
		int groupId = Group.getGroupIdByName(group);
		if (groupId < 0)
			return;
		try {
			Connection conn = DBConnectionFactory.getConnection();
//			PreparedStatement stmt = conn
//					.prepareStatement("insert into ds_comment"
//							+ "(ds_type, ds_id, time, user_id, lang, text, like_total, dislike_total) "
//							+ "values(?, ?, now(), ?, ?, ?, 0, 0)");
			PreparedStatement stmt = conn
					.prepareStatement("insert into ds_comment"
							+ "(ds_type, ds_id, time, group_id, lang, text, like_total, dislike_total) "
							+ "values(?, ?, now(), ?, ?, ?, 0, 0) on duplicate key update text=?,time=now()");
			stmt.setString(1, ds_type);
			stmt.setInt(2, ds_id);
			stmt.setInt(3, groupId);
			stmt.setString(4, lang);
			stmt.setString(5, text);
			stmt.setString(6, text);
			stmt.executeUpdate();
			stmt.close();
			conn.close();
			CustomDataset.updateCustomDatasetDate(ds_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
//	/**
//	 * for custom dataset and uri dataset, only one description in one language
//	 * @param datasetType
//	 * @param datasetId
//	 * @param language
//	 * @return
//	 */
//	public static DatasetComment getDescription(String datasetType, int datasetId, String language){
//		DatasetComment comment = null;
//		
//		if(datasetType.contentEquals(DatasetItem.CUSTOM_GENRE)){
//			comment = getCustomDescription(datasetId, language);
//		}else if(datasetType.contentEquals(DatasetItem.URI_GENRE)){
//			comment = getUriDescription(datasetId, language);
//		}
//		return comment;
//	}
//	/**
//	 * if length<0, get all descriptions
//	 * @param datasetType
//	 * @param datasetId
//	 * @param start
//	 * @param length
//	 * @return
//	 */
//	public static ArrayList<DatasetComment> getDescriptions(String datasetType, int datasetId, int start, int length){
//		ArrayList<DatasetComment> comments = new ArrayList<DatasetComment>();
//		if(datasetType.contentEquals(DatasetItem.CUSTOM_GENRE)){
//			comments = getCustomDescriptions(datasetId, start, length);
//		}else if(datasetType.contentEquals(DatasetItem.URI_GENRE)){
//			comments = getUriDescriptions(datasetId, start, length);
//		}
//		return comments;
//	}

	public static DatasetComment getCustomDescriptionItem(int datasetId, String language){
		DatasetComment comment = null;
		if(language == null){
			language = "en";
		}
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("SELECT id, time, text, like_total, dislike_total from ds_comment where ds_type=? and ds_id=? and lang=? order by time desc;");
			pstmt.setString(1, DatasetItem.CUSTOM_GENRE);
			pstmt.setInt(2, datasetId);
			pstmt.setString(3, language);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				comment = new DatasetComment(rs.getInt(1), DatasetItem.CUSTOM_GENRE, datasetId, rs.getDate(2), language, rs.getString(3), rs.getInt(4), rs.getInt(5));
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comment;
	}
	public static String getCustomDescription(int datasetId, String language){
//		DatasetComment comment = null;
		String comment = null;
		if(language == null){
			language = "en";
		}
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("SELECT id, time, text, like_total, dislike_total from ds_comment where ds_type=? and ds_id=? and lang=? order by time desc;");
			pstmt.setString(1, DatasetItem.CUSTOM_GENRE);
			pstmt.setInt(2, datasetId);
			pstmt.setString(3, language);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				comment = rs.getString("text");
//				comment = new DatasetComment(rs.getInt(1), DatasetItem.CUSTOM_GENRE, datasetId, rs.getDate(2), language, rs.getString(3), rs.getInt(4), rs.getInt(5));
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comment;
	}
	public static ArrayList<DatasetComment> getCustomDescriptions(int datasetId, int start, int length){
		ArrayList<DatasetComment> comments = new ArrayList<DatasetComment>();
		try {
			Connection conn = DBConnectionFactory.getConnection();
			StringBuffer sql = new StringBuffer("SELECT id, time, text, like_total, dislike_total, lang from ds_comment where ds_type=? and ds_id=? order by time desc;");
			if(length > 0){
				sql.append(String.format(" limit %d,%d", start, length));
			}
			PreparedStatement pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1, DatasetItem.CUSTOM_GENRE);
			pstmt.setInt(2, datasetId);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				DatasetComment dc = new DatasetComment(rs.getInt(1), DatasetItem.CUSTOM_GENRE, datasetId, rs.getDate(2), rs.getString(6), rs.getString(3), rs.getInt(4), rs.getInt(5));
				if(!comments.contains(dc)){
					comments.add(dc);
				}
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comments;
	}
	public static DatasetComment getUriDescriptionItem(int datasetId, String language){
		DatasetComment comment = null;
		if(language == null){
			language = "en";
		}
		ArrayList<String> properties = PropertyList.getDescriptionProperties();//.getCommentProperties();
		ArrayList<String> array = new ArrayList<String>();
		for(String str:properties){
			try {
				int p = DBOperations.getURIID(str,true);
				array.add("u"+p) ;
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		int s = datasetId;//DBOperations.getURIID(uri,false);
		int doc_id = datasetId;//DBOperations.getDereferenceDocID(uri);

		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = null;
			if(language.equals("en")){
				stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and ((lang like 'en%') or (lang='' )) and o like 'l%'");
	            stmt.setString(1, "u"+s) ;
				stmt.setInt(2, doc_id);
			}else if(language.contentEquals("zh")){
				stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and (lang like 'zh%') and o like 'l%'");
	            stmt.setString(1, "u"+s) ;
				stmt.setInt(2, doc_id);
			}else{
				stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and lang=? and o like 'l%'");
			    stmt.setString(1, "u"+s) ;
				stmt.setInt(2, doc_id);
				stmt.setString(4, language) ;
			}
			
			for(String property:array){
	//			System.out.println("pp:"+property);
				stmt.setString(3, property) ;
				ResultSet rs = stmt.executeQuery();
				if(rs.next()){
					String text = rs.getString("o").substring(1);
					comment = new DatasetComment(-1, DatasetItem.URI_GENRE, datasetId, null, language, text, -1, -1);
					break;
				}
				rs.close();
			}
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comment;		
	}
	public static String getUriDescription(int datasetId, String language){
//		DatasetComment comment = null;
		String comment = null;
		if(language == null){
			language = "en";
		}
		ArrayList<String> properties = PropertyList.getDescriptionProperties();//.getCommentProperties();
		ArrayList<String> array = new ArrayList<String>();
		for(String str:properties){
			try {
				int p = DBOperations.getURIID(str,true);
				array.add("u"+p) ;
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		int s = datasetId;//DBOperations.getURIID(uri,false);
		int doc_id = datasetId;//DBOperations.getDereferenceDocID(uri);

		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = null;
			if(language.equals("en")){
				stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and ((lang like 'en%') or (lang='' )) and o like 'l%'");
	            stmt.setString(1, "u"+s) ;
				stmt.setInt(2, doc_id);
			}else if(language.contentEquals("zh")){
				stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and (lang like 'zh%') and o like 'l%'");
	            stmt.setString(1, "u"+s) ;
				stmt.setInt(2, doc_id);
			}else{
				stmt = conn.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and lang=? and o like 'l%'");
			    stmt.setString(1, "u"+s) ;
				stmt.setInt(2, doc_id);
				stmt.setString(4, language) ;
			}
			
			for(String property:array){
	//			System.out.println("pp:"+property);
				stmt.setString(3, property) ;
				ResultSet rs = stmt.executeQuery();
				if(rs.next()){
					String text = rs.getString("o").substring(1);
					comment = text;
//					comment = new DatasetComment(-1, DatasetItem.URI_GENRE, datasetId, null, language, text, -1, -1);
					break;
				}
				rs.close();
			}
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comment;		
	}
	
	public static ArrayList<DatasetComment> getUriDescriptions(int datasetId, int start, int length){
		ArrayList<DatasetComment> comments = new ArrayList<DatasetComment>();
		ArrayList<String> properties = PropertyList.getDescriptionProperties();//.getCommentProperties();
		ArrayList<String> array = new ArrayList<String>();
		for(String str:properties){
			try {
				int p = DBOperations.getURIID(str,true);
				array.add("u"+p) ;
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		int s = datasetId;//DBOperations.getURIID(uri,false);
		int doc_id = datasetId;//DBOperations.getDereferenceDocID(uri);

		try {
			Connection conn = DBConnectionFactory.getConnection();
			StringBuffer sql = new StringBuffer("SELECT o,lang FROM quadruple WHERE s=? and doc_id=? and p=?  and o like 'l%'");
			if(length > 0){
				sql.append(String.format(" limit %d, %d", start,length));
			}
			PreparedStatement stmt = conn.prepareStatement(sql.toString());
	        stmt.setString(1, "u"+s) ;
			stmt.setInt(2, doc_id);
			
			for(String property:array){
	//			System.out.println("pp:"+property);
				stmt.setString(3, property) ;
				ResultSet rs = stmt.executeQuery();
				while(rs.next()){
					String text = rs.getString("o").substring(1);
					String lang = rs.getString("lang");
					if(lang.startsWith("en")){
						lang = "en";
					}else if(lang.startsWith("zh")){
						lang = "zh";
					}
					DatasetComment dc = new DatasetComment(-1, DatasetItem.URI_GENRE, datasetId, null, lang, text, -1, -1);
					if(!comments.contains(dc)){
						comments.add(dc);
					}
				}
				rs.close();
			}
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comments;		
	}
	
//	public static ArrayList<DatasetComment> getCommentsByDatasetLangUserOrderByScore(
//			String datasetType, int datasetId, String language, String group,
//			int start, int length) {
//		ArrayList<DatasetComment> comments = new ArrayList<DatasetComment>();
//
////		StringBuffer sqlBuffer = new StringBuffer(
////				String.format(
////						"select id, time, user_id, lang, text, like_total, dislike_total from ds_comment "
////								+ "where ds_type = '%s' and ds_id = %d ",
////						datasetType, datasetId));
//		StringBuffer sqlBuffer = new StringBuffer(
//				String.format(
//						"select id, time, group_id, lang, text, like_total, dislike_total from ds_comment "
//								+ "where ds_type = '%s' and ds_id = %d ",
//						datasetType, datasetId));
//		if (group != null) {
//			int gid = Group.getGroupIdByName(group);
//			sqlBuffer.append(String.format("and group_id = %d ", gid));
//		}
//		if (language != null) {
//			sqlBuffer.append(String.format("and lang = '%s' ", language));
//		}
//		sqlBuffer.append(String.format(
//				"order by like_total desc, dislike_total " + "limit %d, %d",
//				start, length));
////		sqlBuffer.append(String.format(
////				"order by time desc " + "limit %d, %d",
////				start, length));
//		try {
//			Connection conn = DBConnectionFactory.getConnection();
//			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(sqlBuffer.toString());
//			while (rs.next()) {
//				int id = rs.getInt(1);
//				Date time = rs.getDate(2);
//				String mgroup = Group.getGroupNameById(rs.getInt(3));//User.getUserNameById(rs.getInt(3));
//				String lang = rs.getString(4);
//				String text = rs.getString(5);
//				int like_total = rs.getInt(6);
//				int dislike_total = rs.getInt(7);
//				DatasetComment comment = new DatasetComment(id, datasetType,datasetId, 
//						time, mgroup, lang, text, like_total,
//						dislike_total);
//				comments.add(comment);
//			}
//			rs.close();
//			stmt.close();
//			conn.close();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return comments;
//	}
//
//	public static String getOriginDescription(int uri_id,String lang){
//		try {
//			return DatasetComment.getOriginDescription(DBOperations.getURI(uri_id), lang);//.getOriginComment(DBOperations.getURI(uri_id), lang);
//		} catch (Throwable e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//	}
//	public static String getOriginComment(int uri_id,String lang){
//		try {
//			return DatasetComment.getOriginComment(DBOperations.getURI(uri_id), lang);
//		} catch (Throwable e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//	}
//	public static String getOriginDescription(String uri,String lang) throws Throwable {
//		String comment = "";
//		if (uri == null) {
//			return comment;
//		}
//		ArrayList<String> properties = PropertyList.getDescriptionProperties();//.getCommentProperties();
//		ArrayList<String> array = new ArrayList<String>();
//		for(String str:properties){
//			int p = DBOperations.getURIID(str,true) ;
//			array.add("u"+p) ;
//		}
//		
//		int s = DBOperations.getURIID(uri,false);
//		int doc_id = DBOperations.getDereferenceDocID(uri);
//		Connection conn = DBConnectionFactory.getConnection();
//		PreparedStatement stmt = null;
//		if(lang==null){
//			stmt = conn
//			 .prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and o like 'l%'");
//            stmt.setString(1, "u"+s) ;
//			stmt.setInt(2, doc_id);
//		}else if(lang.equals("en")){
//			stmt = conn
//			.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and ((lang like 'en%') or (lang='' )) and o like 'l%'");
//            stmt.setString(1, "u"+s) ;
//			stmt.setInt(2, doc_id);
//		}else{
//			stmt = conn
//			.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and lang=? and o like 'l%'");
//		    stmt.setString(1, "u"+s) ;
//			stmt.setInt(2, doc_id);
//			stmt.setString(4, lang) ;
//		}
//		
//		for(String property:array){
////			System.out.println("pp:"+property);
//			stmt.setString(3, property) ;
//			ResultSet rs = stmt.executeQuery();
//			if(rs.next()){
//				comment = rs.getString("o").substring(1);
//				break;
//			}
//			rs.close();
//		}
//		stmt.close();
//		conn.close();
//		
//		return comment;		
//	}
//	public static String getOriginComment(String uri,String lang) throws Throwable {
//		String comment = "";
//		if (uri == null) {
//			return comment;
//		}
//
//		ArrayList<String> properties = PropertyList.getCommentProperties();
//		ArrayList<String> array = new ArrayList<String>();
//		for(String str:properties){
//			int p = DBOperations.getURIID(str,true) ;
//			array.add("u"+p) ;
//		}
//		
//		int s = DBOperations.getURIID(uri,false);
//		int doc_id = DBOperations.getDereferenceDocID(uri);
//		Connection conn = DBConnectionFactory.getConnection();
//		PreparedStatement stmt = null;
//		if(lang==null){
//			stmt = conn
//			 .prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and o like 'l%'");
//            stmt.setString(1, "u"+s) ;
//			stmt.setInt(2, doc_id);
//		}else if(lang.equals("en")){
//			stmt = conn
//			.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and ((lang like 'en%') or (lang='' )) and o like 'l%'");
//            stmt.setString(1, "u"+s) ;
//			stmt.setInt(2, doc_id);
//		}else{
//			stmt = conn
//			.prepareStatement("SELECT o FROM quadruple WHERE s=? and doc_id=? and p=?  and lang=? and o like 'l%'");
//		    stmt.setString(1, "u"+s) ;
//			stmt.setInt(2, doc_id);
//			stmt.setString(4, lang) ;
//		}
//		
//		for(String property:array){
//			stmt.setString(3, property) ;
//			ResultSet rs = stmt.executeQuery();
//			if(rs.next()){
//				comment = rs.getString("o").substring(1);
//				break;
//			}
//			rs.close();
//		}
//		stmt.close();
//		conn.close();
//		
//		return comment;		
//	}
//	
//	public static ArrayList<DatasetComment> getCommentsByDatasetLangUserOrderByTime(
//			String datasetType, int datasetId, String language, String group,
//			int start, int length) {
//		ArrayList<DatasetComment> comments = new ArrayList<DatasetComment>();
//
////		StringBuffer sqlBuffer = new StringBuffer(
////				String.format(
////						"select id, time, user_id, lang, text, like_total, dislike_total from ds_comment "
////								+ "where ds_type = '%s' and ds_id = %d ",
////						datasetType, datasetId));
//		StringBuffer sqlBuffer = new StringBuffer(
//				String.format(
//						"select id, time, group_id, lang, text, like_total, dislike_total from ds_comment "
//								+ "where ds_type = '%s' and ds_id = %d ",
//						datasetType, datasetId));
//		if (group != null) {
////			int uid = User.getUserIdByName(author);
//			int gid = Group.getGroupIdByName(group);
//			sqlBuffer.append(String.format("and group_id = %d ", gid));
//		}
//		if (language != null) {
//			sqlBuffer.append(String.format("and lang = '%s' ", language));
//		}
//		sqlBuffer.append("order by time desc ");
//		if(length>0){
//			sqlBuffer.append(String.format("limit %d, %d",start, length));
//		}
//
//		try {
//			Connection conn = DBConnectionFactory.getConnection();
//			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(sqlBuffer.toString());
//			while (rs.next()) {
//				int id = rs.getInt(1);
//				Date time = rs.getDate(2);
//				String mgroup = Group.getGroupNameById(rs.getInt(3));//User.getUserNameById(rs.getInt(3));
//				String lang = rs.getString(4);
//				String text = rs.getString(5);
//				int like_total = rs.getInt(6);
//				int dislike_total = rs.getInt(7);
//				DatasetComment comment = new DatasetComment(id, datasetType,datasetId, 
//						time, mgroup, lang, text, like_total,
//						dislike_total);
//				comments.add(comment);
//			}
//			rs.close();
//			stmt.close();
//			conn.close();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return comments;
//	}

	public static int getDislikeTotalByCommentID(int commentId) {
		int result = 0;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(String.format(
					"select dislike_total from ds_comment " + "where id= %d",
					commentId));
			if (rs.next()) {
				result = rs.getInt("dislike_total");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static int getLikeTotalByCommentID(int commentId) {
		int result = 0;
		try {
			Connection conn = DBConnectionFactory.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(String.format(
					"select like_total from ds_comment " + "where id= %d",
					commentId));
			if (rs.next()) {
				result = rs.getInt("like_total");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * add liketotal
	 * 
	 * @param commentId
	 * @param likeNum
	 *            . The new like num
	 */
	public static void addLikeTotal(int commentId, int likeNum) {
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn
					.prepareStatement("update ds_comment set like_total=like_total+? where id=?");
			stmt.setInt(1, likeNum);
			stmt.setInt(2, commentId);
			stmt.executeUpdate();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * add disliketotal
	 * 
	 * @param commentId
	 * @param likeNum
	 *            . The new dislike num
	 */
	public static void addDislikeTotal(int commentId, int likeNum) {
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement stmt = conn
					.prepareStatement("update ds_comment set dislike_total=dislike_total+? where id=?");
			stmt.setInt(1, likeNum);
			stmt.setInt(2, commentId);
			stmt.executeUpdate();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
